﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.InsuranceWarranty;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.AuditConfig;
using ZBJF.Warranty.Model.ViewModel.InsuranceWarranty;

namespace ZBJF.Warranty.BLL
{
    public partial class AuditProjectConfigurationService
    {

        #region 根据主键ID获取项目审核配置 + AuditProjectConfiguration LoadByID(int id)
        public AuditProjectConfiguration LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断项目审核配置是否存在 + bool IsExist(long productTypeID, int accessoriesTypeID, string AuditProjectConfigurationName)
        /// <summary>
        ///判断项目审核配置是否存在
        /// </summary>
        /// <param name="auditProjectWithType">审核项目编码</param>
        /// <param name="projectName">项目字段名称</param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsExist(long auditProjectWithType, string projectName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<AuditProjectConfiguration>();
            if (id > 0)
            {
                expressionWhere = expressionWhere.And(o => o.ID != id);
            }
            expressionWhere = expressionWhere.And(o => o.AuditProjectWithType == auditProjectWithType);
            expressionWhere = expressionWhere.And(o => o.ProjectName == projectName);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取项目审核配置列表 + IQueryable<AuditProjectConfiguration> GetPagingList(AuditProjectConfigurationModel model, out int total)
        /// <summary>
        /// 分页获取项目审核配置列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<AuditProjectConfiguration> GetPagingList(AuditProjectConfigurationModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<AuditProjectConfiguration>();

            //设备类型名称
            if (model.AuditProjectWithType > 0)
            {
                expressionWhere = expressionWhere.And(o => o.AuditProjectWithType == model.AuditProjectWithType);
            }
            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<AuditProjectConfiguration>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion

        #region 根据审核项目删除审核项目选项 + bool DeleteAuditProjectOption(int id)
        /// <summary>
        /// 根据审核项目删除审核项目选项
        /// </summary>
        /// <param name="id">审核项目ID</param>
        /// <returns></returns>
        public bool DeleteAuditProjectOption(int id)
        {
            string sql = "DELETE FROM AuditProjectFieldOption WHERE AuditProjectConfigurationID=@AuditProjectConfigurationID";
            return ExcuteSql(sql, new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("AuditProjectConfigurationID", id) }) > 0;
        }

        #endregion

        #region 获取审核项目 + List<AuditProjectConfigurationSelectModel> GetAllAuditModel()
        /// <summary>
        /// 获取审核项目
        /// </summary>
        /// <returns></returns>
        public List<AuditProjectConfigurationSelectModel> GetAllAuditModel()
        {
            return LoadEntities(t => t.IsAvailable == true).AsNoTracking().ToList()
                .Select(t => new AuditProjectConfigurationSelectModel
                {
                    ProjectName = t.ProjectName,
                    ID = t.ID,
                    AuditProjectWithType = Convert.ToInt32(-t.AuditProjectWithType),//把项目类型转成负数，避免与审核项目ID重复
                    FieldType = t.FieldType
                }).ToList();
        }
        #endregion
    }
}
